package com.z.system.dto;

import java.util.Date;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.z.system.model.Role;

/**
 * 角色数据传输对象，用于将角色实体转换为前端所需的格式
 */
public class RoleDTO {
    private String roleId;
    private String roleName;
    private String roleCode;
    private String description;
    private Boolean enabled;
    private String createTime;

    /**
     * 无参构造函数，用于Jackson JSON序列化和反序列化
     */
    public RoleDTO() {
    }

    /**
     * 构造函数，用于Spring Data JPA直接从数据库查询创建DTO
     */
    public RoleDTO(UUID id, String name, String code, String remark, Integer del) {
        // 将UUID转换为String
        this.roleId = id != null ? id.toString() : null;
        this.roleName = name;
        this.roleCode = code;
        this.description = remark;
        // 将del字段转换为enabled（1表示启用，0表示禁用）
        this.enabled = del != null && del == 0;
    }

    /**
     * 构造函数，用于Spring Data JPA直接从数据库查询创建DTO（包含创建时间）
     */
    public RoleDTO(UUID id, String name, String code, String remark, Integer del, Date createTime) {
        // 调用基础构造函数
        this(id, name, code, remark, del);
        // 将Date转换为字符串格式
        this.createTime = createTime != null ? createTime.toString() : null;
    }

    // 转换为角色实体的方法
    public Role toRole() {
        Role role = new Role();
        if (StringUtils.hasText(roleId) && !roleId.equals("0")) {
            role.setId(UUID.fromString(roleId));
        }
        role.setName(roleName);
        role.setCode(roleCode);
        role.setRemark(description);
        role.setDel(enabled ? 0 : 1);
        return role;
    }

    // Getters and Setters

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRoleCode() {
        return roleCode;
    }

    public void setRoleCode(String roleCode) {
        this.roleCode = roleCode;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }
}